package groovy.jmx.builder

import groovy.jmx.builder.node.AttrNodeData

class JmxAttributeFactory extends AbstractFactory {
    private AttrNodeData data

    public Object newInstance(FactoryBuilderSupport builder, Object nodeName, Object nodeArgs, Map nodeAttribs) {
        if (!nodeAttribs.containsKey("name")) {
            throw new JmxBuilderException("You must provide at least a name for the attribute.")
        }
        data = new AttrNodeData(
                name: nodeAttribs.remove('name'),
                description: nodeAttribs.remove('description') ?: nodeAttribs.remove('desc'),
                defaultValue: nodeAttribs.remove('default'),
                writable: nodeAttribs.remove('writable') ?: nodeAttribs.remove('editable') ?: false,
                value: nodeAttribs
        )
        return data
    }

    public boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map nodeAttribs) {
        return true;
    }

    public void setParent(FactoryBuilderSupport builder, Object parentNode, Object childNode) {
        parentNode.attributes.add(childNode)
    }

    public boolean isLeaf() {
        return true
    }
}
